<template>
  <el-form :model="form" ref="formRef" :rules="formRules" v-loading="loading">
    <el-form-item label="姓名" prop="name">
      <el-input placeholder="请输入" v-model="form.name"></el-input>
    </el-form-item>
    <el-form-item label="电话" prop="phone">
      <el-input placeholder="请输入" v-model="form.phone"></el-input>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import { getTeacherOne } from "../../api/index.ts";
import { onMounted, ref } from "vue";

const props = withDefaults(defineProps<{ id?: number }>(), {
  id: null,
});

const form = ref({ name: "", phone: "" });
const formRef = ref(null);
const loading = ref(false);
const formRules = ref({
  name: [
    {
      required: true,
      message: "请输入姓名",
    },
  ],
  phone: [
    {
      required: true,
      message: "请输入电话",
    },
  ],
});

const initData = () => {
  loading.value = true;
  getTeacherOne(props.id)
    .then((res) => {
      console.log("🚀 ~ getTeacherOne ~ res:", res);
      const data = res[0];
      Object.keys(data).forEach((key) => {
        if (form.value.hasOwnProperty(key)) {
          form.value[key] = data[key];
        }
      });
    })
    .finally(() => {
      loading.value = false;
    });
};

onMounted(() => {
  if (props.id) initData();
});

defineExpose({
  form,
  formRef,
});
</script>

<style scoped></style>
